define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function ($, undefined, Backend, Table, Form, Template) {

    var Controller = {
        index: function () {
            require(['bootstrap-select', 'bootstrap-select-lang']);
            // 提交生成
            $('#form-controller .btn-submit').click(function (e) {
                $('#form-controller').trigger('validate');
            });
            $('#form-controller').on('valid.form', function (e, form) {
                Layer.confirm('确认生成控制器？', {
                    title: '操作确认',
                    btn: ['确认', '取消']
                }, function (index) {
                    Fast.api.ajax({
                        url: 'helper/generator/index',
                        data: $('#tab-controller form').serialize(),
                        complete: function () {
                            layer.close(index);
                        }
                    }, function (data, ret) {
                        layer.open({
                            icon: 1,
                            title: '小程序控制器生成成功',
                            content: '如小程序中未显示该菜单，请在<code>「菜单管理」</code>中打开。'
                        });
                        return false;
                    }, function (data, ret) {
                        layer.open({
                            icon: 2,
                            title: '生成失败',
                            content: ret.msg
                        });
                        return false;
                    });
                }, function (index) {
                    layer.close(index);
                });
            });
            $("form [data-toggle='popover']").popover({
                placement: 'auto',
                trigger: 'hover',
                html: true,
            });
            // 折叠步骤区域
            $('legend text').click(function () {
                $(this).parent().next().slideToggle(100);
            });
            // 选择菜单规则
            $(document).on('change', "select[name='menu']", function () {
                var that = this;
                $("#menu-tips").toggleClass('hide', $(that).val() === '0');
                if ($(that).val() === '0') return;
                var rule = $(that).find('option:selected').data('name');
                var rulePath = rule.substr(0, rule.lastIndexOf('/') + 1);
                var fileName = rule.substr(rule.lastIndexOf('/') + 1);
                fileName = fileName.replace(fileName[0], fileName[0].toUpperCase());
                var controllerPath = `/addons/helper/controller/wxapp/${rulePath}${fileName}.php`;
                $('#menu-tips strong').text(controllerPath);
                return false;
            });
            // 选择主表
            $('select[name="table"]').on('changed.bs.select', function () {
                var that = this;
                $('#field-zone,#config-zone').removeClass('hide');
                Fast.api.ajax({
                    url: 'helper/generator/getFieldList',
                    data: { table: $(that).val() },
                }, function (data, ret) {
                    mainfields = data.fieldList;
                    $('input[name="model"]').val(data.TABLE_COMMENT);
                    renderTabsSelect(data.tabList);
                    renderStatusFieldSelect(data.tabList);
                    renderFieldSelect(data.fieldList);
                    renderFieldTable(data.fieldList);
                    return false;
                });
                return false;
            });
            // 功能按钮选择绑定事件
            $('.select-buttons').on('changed.bs.select', function () {
                $('.field-sort').toggleClass('hide', $.inArray('sort', $(this).val()) === -1);
                $('.field-filter').toggleClass('hide', $.inArray('filter', $(this).val()) === -1);
            });
            var renderTabsSelect = function (tabList) {
                var content = Template('tabstpl', { tabList: tabList });
                $('#tabs').html(content);
                $('#tabs .selectpicker').selectpicker();
            };
            var renderStatusFieldSelect = function (tabList) {
                var content = Template('statusfieldtpl', { tabList: tabList });
                $('#statusField').html(content);
                $('#statusField .selectpicker').selectpicker();
            };
            var renderFieldSelect = function (fieldList) {
                var content = Template('fieldtpl', { fieldList: fieldList });
                $('#listField,#listTitleField,#listImageField').empty();
                $(content).appendTo($('#listField,#listTitleField,#listImageField'));
                $('#listField,#listTitleField,#listImageField').selectpicker('refresh');
            };
            var renderFieldTable = function (fieldList) {
                var content = Template('fieldzonetpl', { fieldList: fieldList });
                // 渲染数据行
                $('#field-zone tbody').html(content);
                // 渲染数据行中的selectpicker
                $('#field-zone tbody .selectpicker').selectpicker();
                // 控制筛选语法select的隐显
                $('#field-zone tbody input[name*="[filterable]"]').on('change', function () {
                    var that = this;
                    var field = $(that).data('name');
                    $(`#field-zone tbody select[name="field[${field}][op]"]`).parents('.selectpicker-warpper').toggleClass('hide', $(that).val() === '0');
                });
                $("#field-zone table [data-toggle='popover']").popover({
                    placement: 'auto',
                    trigger: 'hover',
                    html: true,
                });
            };
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent(
                    $("form[role=form]"), null, null,
                    function () {
                        return false;
                    }
                );
            }
        }
    };
    return Controller;
});
